﻿using System.Linq;
using System.Web.Mvc;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using QDT.Common;
using QDT.Core.Projects;
using QDT.MVC.Security;
using QDT.Services.Projects;
using QDT.Web.Admin.Models;

namespace QDT.Web.Admin.Controllers {
    [SecurityModule(Name = "项目分类管理")]
    public class ProjectTypeController : BaseController {
        private readonly IProjectTypeService _typeService;

        public ProjectTypeController(IProjectTypeService projectTypeService) {
            _typeService = projectTypeService;
        }

        [SecurityNode(Name = "分类列表")]
        public ActionResult Index(int? page, GridSortOptions sort)
        {
            const int pageSize = 10;
            int pageNumber = page ?? 1;

            if (sort.Column == null) sort.Column = "ID";

            ViewData["sort"] = sort;

            IQueryable<ProjectType> source = DbProvider.D<ProjectType>().Where(x => !x.IsDelete).OrderBy(sort.Column, sort.Direction = SortDirection.Ascending);

            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        [SecurityNode(Name = "分类回收站")]
        public ActionResult Trash(int? page, GridSortOptions sort)
        {
            const int pageSize = 10;
            int pageNumber = page ?? 1;

            if (sort.Column == null) sort.Column = "ID";

            ViewData["sort"] = sort;

            IQueryable<ProjectType> source = DbProvider.D<ProjectType>().Where(x => x.IsDelete).OrderBy(sort.Column, sort.Direction = SortDirection.Ascending);

            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        [SecurityNode(Name = "添加分类")]
        public ActionResult AddType()
        {
            return View();
        }

        [HttpPost]
        [SecurityNode(Name = "添加分类操作")]
        public ActionResult PostAddType(ProjectTypeModel model)
        {
            if (!ModelState.IsValid) return Error();

            var type = new ProjectType(model.Name, model.Brief, model.IsEffect, model.Icon) {
                                                                                                Sort = model.Sort
                                                                                            };
            _typeService.CreateType(type);
            return Success("添加成功");
        }

        [SecurityNode(Name = "编辑分类")]
        public ActionResult EditType(int id)
        {
            var type = DbProvider.GetByID<ProjectType>(id);

            return View(type);
        }

        [HttpPost]
        [SecurityNode(Name = "编辑分类操作")]
        public ActionResult PostEditType(ProjectTypeModel model)
        {
            if (!ModelState.IsValid) return Error();

            var type = DbProvider.GetByID<ProjectType>(model.ID);

            if (type == null) return Error("项目类型不存在");

            type.Name = model.Name;
            type.Brief = model.Brief;
            type.Sort = model.Sort;
            type.Icon = model.Icon;
            type.IsEffect = model.IsEffect;

            DbProvider.SaveChanges();


            return Success("编辑成功");
        }

        [HttpPost]
        [SecurityNode(Name = "删除分类")]
        public ActionResult Delete(int[] id)
        {
            _typeService.DeleteType(id);
            return Success("删除成功");
        }

        [SecurityNode(Name = "恢复分类")]
        public ActionResult Restore(int[] id)
        {
            _typeService.RestoreType(id);
            return Success("恢复成功");
        }

        [SecurityNode(Name = "永久删除分类")]
        public ActionResult ForeverDel(int[] id)
        {
            _typeService.ForeverDeleteType(id);
            return Success("彻底删除成功");
        }

        public ActionResult FeildList(int cid, int? page, GridSortOptions sort) {

            var cate = DbProvider.GetByID<ProjectType>(cid);

            if (cate == null) return Error("贷款类型不存在");

            ViewData["ProjectType"] = cate;

            const int pageSize = 10;

            int pageNumber = page ?? 1;

            if (sort.Column == null) sort.Column = "ID";


            ViewData["sort"] = sort;
            IQueryable<ProjectFeild> source = DbProvider.D<ProjectFeild>().Where(x => x.TypeID == cid).OrderBy(sort.Column, sort.Direction = SortDirection.Ascending);

            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        public ActionResult AddFeild(int cateID) {

            var model = new ProjectFeildModel {
                                               TypeID = cateID,
                                               IsEffect = true
                                           };

            return View(model);
        }

        [HttpPost]
        public ActionResult PostAddFeild(ProjectFeildModel model) {
            
            if (!ModelState.IsValid) return Error();

            var type = DbProvider.GetByID<ProjectType>(model.TypeID);

            if (type == null) return Error("项目类型不存在");

            var feild = new ProjectFeild(type,model.Name,model.ShowName,model.InputType) {
                                                                                             ValueScope = model.ValueScope,
                                                                                             Tip = model.Tip,
                                                                                             IsEffect = model.IsEffect,
                                                                                             Sort = model.Sort
                                                                                         };

           _typeService.CreateFeild(feild);

            return Success("操作成功");
        }

        public ActionResult EditFeild(int id) {
            var feild = DbProvider.GetByID<ProjectFeild>(id);

            if (feild == null) return Error("扩展字段不存在");

            return View(feild);
        }

        [HttpPost]
        public ActionResult PostEditFeild(ProjectFeildModel model)
        {
            if (!ModelState.IsValid) return Error();

            var feild = DbProvider.GetByID<ProjectFeild>(model.ID);

            if (feild == null) return Error("扩展字段不存在");

            feild.Name = model.Name;

            feild.ShowName = model.ShowName;

            feild.InputType = model.InputType;

            feild.ValueScope = model.ValueScope;

            feild.Tip = model.Tip;

            feild.IsEffect = model.IsEffect;

            feild.Sort = model.Sort;

            _typeService.UpdateFeild(feild);

            return Success("操作成功");
        }

        [HttpPost]
        public ActionResult DeleteFeild(int[] id) {

            _typeService.DeleteFeild(id);

            return Success("彻底删除成功");
        }

    }
}